package com.jinzecloud.reportapproval.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.jinzecloud.reportapproval.bean.ConsCheckRecord;
import com.jinzecloud.reportapproval.bean.ConsCheckRecordInfo;
import com.jinzecloud.reportapproval.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.StandardCopyOption;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jinzecloud.reportapproval.mapper.BreakageRateMapper;
import com.jinzecloud.reportapproval.bean.BreakageRate;
import com.jinzecloud.reportapproval.service.BreakageRateService;

import javax.servlet.http.HttpServletRequest;


@Service("breakageRateService")
public class BreakageRateServiceImpl extends ServiceImpl<BreakageRateMapper, BreakageRate> implements BreakageRateService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    private SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd"); // 原始格式


    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日");

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BreakageRate> page = this.page(
                new Query<BreakageRate>().getPage(params),
                new QueryWrapper<BreakageRate>()
        );

        return new PageUtils(page);
    }

    @Override
    public String viewPdf(Integer id) {

        BreakageRate breakageRate = getById(id);

        String dataTime = breakageRate.getDetectionTime();
        try {
            breakageRate.setDetectionTime(sdf.format(inputFormat.parse(dataTime)));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(breakageRate));

        //word
        File tempFile = WordUtil.getWordFile("template/breakageRate.docx", data, Configure.builder().useSpringEL());

        if (tempFile == null) return null;
        String fileName = "碎土率测定记录表";
        String uploadDirectory = fileUploadUtil.getUploadFilePath("temp");
        String filePath = uploadDirectory + breakageRate.getProjectCode() + fileName;
        String savePath = RequestUtils.getBasePath(request) + "upload/temp/" + breakageRate.getProjectCode() +fileName;
        FileUtil.copyFile(tempFile, new File(filePath + ".docx"), StandardCopyOption.REPLACE_EXISTING);
        PDFUtil.WordToPdf(tempFile, new File(filePath + ".pdf"));

        update(new LambdaUpdateWrapper<BreakageRate>().set(BreakageRate::getWordPath, savePath + ".docx").set(BreakageRate::getPdfPath, savePath + ".pdf").eq(BreakageRate::getId, id));

        return RequestUtils.getBasePath(request) + "upload/temp/" + breakageRate.getProjectCode() + fileName + ".pdf";
    }

    @Override
    public JSONObject checkRecordJson(String projectUuid) {
        BreakageRate breakageRate = getOne(new QueryWrapper<BreakageRate>().eq("project_code", projectUuid));
        if(breakageRate == null) return null;
        String dataTime = breakageRate.getDetectionTime();
        try {
            breakageRate.setDetectionTime(sdf.format(inputFormat.parse(dataTime)));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(breakageRate));

        return data;
    }

    public BreakageRate getProductNumber(String projectNumber) {
        BreakageRate breakageRate = getOne(new QueryWrapper<BreakageRate>().eq("project_code", projectNumber));
        return breakageRate;
    }
}